package com.hydra.wiki.doc.util;

import com.alibaba.fastjson.JSON;
import com.hydra.wiki.core.auth.domain.LoginUser;
import com.hydra.wiki.doc.domain.Doc;
import com.hydra.wiki.doc.domain.ex.TypeBrief;
import com.hydra.wiki.doc.server.FarmDocOperateRightInter;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Log4j
public class ExcelUtils {

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class DOC{
        private String knowtitle;
        private String knowtype;
        private String text;
        private String knowtag;
        private String knowtypeId;
        private FarmDocOperateRightInter.POP_TYPE pop_type_edit;
        private FarmDocOperateRightInter.POP_TYPE pop_type_read;
        private String groupId;
        private LoginUser currentUser;
    }

    public static List<DOC> getDocList(Workbook workbook, int sheetIndex){
        Sheet sheet = workbook.getSheetAt(sheetIndex);
        int rows = sheet.getPhysicalNumberOfRows();
        // 有数据时才处理
        Row row;
        Cell cell;
        String cellValue = "";
        List<DOC> docList = new ArrayList<>();
        if (rows > 0) {
            for (int i = 1; i < rows; i++) {
                row = sheet.getRow(i);
                DOC.DOCBuilder builder = DOC.builder();
                for (int j = 0; j < 4; j++) {
                    cell = row.getCell(j);
                    if (cell != null){
                        CellType cellType = cell.getCellType();
                        if (cellType == CellType.NUMERIC) {
                            cellValue = String.valueOf(cell.getNumericCellValue());
                        } else if (cellType == CellType.BOOLEAN) {
                            cellValue = String.valueOf(cell.getBooleanCellValue());
                        } else {
                            cellValue = cell.getStringCellValue();
                        }
                        if (cellValue == null || cellValue.equals("")) {
                            continue;
                        }
                    }else {
                        cellValue = "";
                    }
                    if (j == 0){
                        builder.knowtitle(cellValue);
                    }else if (j == 1){
                        builder.text(cellValue);
                    }else if (j == 2){
                        builder.knowtag(cellValue);
                    }else if (j == 3){
                        builder.knowtype(cellValue);
                        DOC build = builder.build();
                        docList.add(build);
                    }
                }
            }
        }
        return docList;
    }

    public static void wrapType(List<DOC> docList, List<TypeBrief> typeBriefList) throws Exception {
        if (CollectionUtils.isNotEmpty(docList)
                && CollectionUtils.isNotEmpty(typeBriefList)){
            for (DOC doc : docList) {
                String typeId = typeBriefList.stream()
                        .filter(typeBrief -> Objects.equals(doc.getKnowtype(), typeBrief.getName()))
                        .findAny().orElseThrow(() -> new Exception("未找到对应的文档分类")).getId();
                doc.setKnowtypeId(typeId);
            }
        }
    }
}
